

function addSensorbitCod(Blockly) {






    //RGB超声波传感器模块初始化
  Blockly.Python.microbit_rgbUltrasonicInit = function(block) {
    var dropdown_ioPort = this.getFieldValue('ioPort');
    var dropdown_rgbPort = this.getFieldValue('rgbPort');
    Blockly.Python.definitions_['import_microbit'] = 'from microbit import *';
    Blockly.Python.definitions_['import_ultrasonic'] = 'from sensor import ultrasonic';
    Blockly.Python.definitions_['import_neopixel'] = 'import neopixel';
    Blockly.Python.definitions_['import_rgbPort'] = 'rgbPort = ' + dropdown_rgbPort ;
    Blockly.Python.definitions_['import_ioPort'] = 'ioPort = ' +dropdown_ioPort;
    Blockly.Python.definitions_['import_rgbUltrasonicInit'] = 'ultrasonic.init_rus_04('+dropdown_ioPort+', '+dropdown_rgbPort+')\n';
    return "";
  }

  //RGB超声波传感器模块获取距离
  Blockly.Python.microbit_rgbUltrasonicDistance = function(block) {
    var code = "ultrasonic.get_distance()";
    return [code];
  }

  //RGB超声波传感器模块
  Blockly.Python.microbit_rgbUltrasonicColor = function(block) {
    var dropdown_rgbLight = this.getFieldValue('rgbLight');
    var dropdown_rgbSpecial = this.getFieldValue('rgbSpecial');
    var dropdown_rgbUlr = 0;
    var dropdown_rgbUlg = 0;
    var dropdown_rgbUlb = 0;
    var dropdown_rgbBrightness = Blockly.Python.valueToCode(this, 'rgbBrightness', Blockly.Python.ORDER_ATOMIC);
    
    var color = '';
    var special = "";
    var brightness = "";
    var dropdown_colour = block.childBlocks_[1].colour_;
      var colorStr = dropdown_colour.colorRgb();
      if (colorStr) {
        var colorArr = colorStr.split(",");
        dropdown_rgbUlr = colorArr[0];
        dropdown_rgbUlg = colorArr[1];
        dropdown_rgbUlb = colorArr[2];
     }
    if(dropdown_rgbLight === 'right'){
      color = 'np[0] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\nnp[1] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\nnp[2] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\n'
      // + 'np[3] = (0, 0, 0)\nnp[4] = (0, 0, 0)\nnp[5] = (0, 0, 0)\n'
      ;
      special = "np."+ dropdown_rgbSpecial +"(0, 2)\n";
      brightness = 'np.brightness(0, 2, '+dropdown_rgbBrightness+')\n';
    }else if(dropdown_rgbLight === 'left'){
      color = 
      // 'np[0] = (0, 0, 0)\nnp[1] = (0, 0, 0)\nnp[2] = (0, 0, 0)\n'
      // +
       'np[3] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\nnp[4] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\nnp[5] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\n';
      special = "np."+ dropdown_rgbSpecial +"(3, 5)\n";
      brightness = 'np.brightness(3, 5, '+dropdown_rgbBrightness+')\n';
    }else{
      color = 'np[0] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\nnp[1] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\nnp[2] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\n'
      +'np[3] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\nnp[4] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\nnp[5] = ('+dropdown_rgbUlg+', '+ dropdown_rgbUlr +', '+dropdown_rgbUlb+')\n'
      ;
      special = "np."+ dropdown_rgbSpecial +"(0, 5)\n";
      brightness = 'np.brightness(0, 5, '+dropdown_rgbBrightness+')\n';
    }
    Blockly.Python.definitions_['import_data'] = 'np = neopixel.NeoPixel(rgbPort, 6)\n'+ color;
    Blockly.Python.definitions_['import_brightness'] = brightness;
    return special;
  }

  String.prototype.colorRgb = function(){
    var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    var sColor = this.toLowerCase();
    if(sColor && reg.test(sColor)){
      if(sColor.length === 4){
        var sColorNew = "#";
        for(var i=1; i<4; i+=1){
          sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));	
        }
        sColor = sColorNew;
      }
      //处理六位的颜色值
      var sColorChange = [];
      for(var i=1; i<7; i+=2){
        sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));	
      }
      return  sColorChange.join(",") ;
    }else{
      return sColor;	
    }
  };

    return Blockly;
}

module.exports = addSensorbitCod;

